package controllers

import (
	"fmt"
	"log"
	"lottery_code/comm"
	"lottery_code/conf"
	"lottery_code/models"
	"lottery_code/services"
	"lottery_code/web/utils"
	"strconv"
	"time"
)

func (api *LuckyApi) checkUserday(uid int,num int64) bool {
	userDayService:=services.NewUserDayService()
	userdayInfo :=userDayService.GetUserToday(uid)
	if userdayInfo != nil && userdayInfo.Uid == uid {
		//今天存在抽奖记录
		if userdayInfo.Num >= conf.UserPrizeMax {
			if int(num)<userdayInfo.Num{
				utils.InitUserLuckyNum(uid,int64(userdayInfo.Num))
			}
			return false
		} else {
			userdayInfo.Num++
			if int(num)<userdayInfo.Num{
				utils.InitUserLuckyNum(uid,int64(userdayInfo.Num))
			}
			err103 := userDayService.Update(userdayInfo, nil)
			if err103 != nil {
				log.Println("index_lucky_check_userday ServiceUserDay.Update err103=", err103)
			}
		}
	} else {
		//创建今天的用户参与记录
		y, m, d := time.Now().Date()
		strDay := fmt.Sprintf("%d%02d%02d", y, m, d)
		day, _ := strconv.Atoi(strDay)
		date := &models.LtUserday{
			Uid:        uid,
			Day:        day,
			Num:        1,
			SysCreated: comm.NowUnix(),
		}
		err := userDayService.Create(date)
		if err != nil {
			log.Println("index_lucky_check_userday ServiceUserDay.Create err=", err)
		}
		utils.InitUserLuckyNum(uid,1)
	}
	return true
}
